<template>
  <div class="content">
    <div class="address">
      <ul>
        <li v-for="(item, index) in billList" v-if="item.state===1">
          <div class="top">
            <div class="tTop">
              <div class="name">
                {{item.companyName}}
              </div>
            </div>
            <div class="dizhi">
              <p>税号：{{item.dutyParagraph}}</p>
              <!--<p>{{item.addressMore}}</p>-->
            </div>
          </div>
          <div class="bottom">
            <div class="select" @click="selected(index)">
              <i :class="item.state===1?'selected':''"></i>
              <b :class="item.state===1?'red':''">设为默认</b>
            </div>
            <div class="operate">
              <div ><img src="../assets/images/ico_bianji@1x.png" alt="" @click="edit(index)">编辑</div>
              <div class="right" @click="remove(index)"><img src="../assets/images/ico_shanchu@1x.png" alt="">删除</div>
            </div>
          </div>
        </li>
        <li v-for="(item, index) in billList" v-if="item.state===0">
          <div class="top">
            <div class="tTop">
              <div class="name">
                {{item.companyName}}
              </div>
            </div>
            <div class="dizhi">
              <p>税号：{{item.dutyParagraph}}</p>
              <!--<p>{{item.addressMore}}</p>-->
            </div>
          </div>
          <div class="bottom">
            <div class="select" @click="selected(index)">
              <i :class="item.state===1?'selected':''"></i>
              <b :class="item.state===1?'red':''">设为默认</b>
            </div>
            <div class="operate">
              <div ><img src="../assets/images/ico_bianji@1x.png" alt="" @click="edit(index)">编辑</div>
              <div class="right" @click="remove(index)"><img src="../assets/images/ico_shanchu@1x.png" alt="">删除</div>
            </div>
          </div>
        </li>
      </ul>
      <div class="btn">
        <a href="javascript:;" class="btn-next" @click="NewBill">添加新发票</a>
      </div>
    </div>
    <alertTip v-if="showAlert" @closeTip="go()" :alertText="alertText"></alertTip>
  </div>

</template>

<script>
  /* eslint-disable semi */
  import wx from 'weixin-js-sdk'
  import { wxHttpTool,wx_config, http_getParam, get_shopQrcoe,wx_share,wx_hideAllMenu } from '../components/httpUtil/httpUtil'
  import {msg_10046} from '../message/msg_10046'
import {relogin} from "../components/httpUtil/httpUtil";

  export default {
    name: '',
    data () {
      return {
        billList: [],
        select: 'selected',
        alertText:null,
        showAlert :false
      }
    },
    methods: {
      alertTip(msg){
        this.showAlert = true;
        this.alertText = msg;
      },
      go(){
        this.showAlert = false;
      },
      getData () {
        const jsonValue = JSON.stringify({
          'c': '10037',
          'p': {
            'userId': JSON.parse(window.localStorage.data).id, // 用户ID
            'tokenId': token()
          }
        });
        this.$http.post(api + '/qqs.us', jsonValue).then((res) => {
          console.log(res);
          const data = res.data.p;
          if (data.isTrue === true) {
            this.billList = data.invoiceList;
//            this.$router.push('/home');
            //          this.$emit('has-log', res.data.username);
          } else {
            this.alertTip(data.errorMsg);
            if (data.errorCode === 90) {
              this.go=()=> {
                this.showAlert = false
                let _this = this
                relogin(_this)
              }
            }
          }
        }, (res) => {
          this.alertTip('网络错误');
          this.go = ()=>{
             this.showAlert = false;
          }
        })
      },
      // 设为默认
      selected (index) {
        console.log(index)
        const jsonValue = JSON.stringify({
          'c': '10038',
          'p': {
            'userId': JSON.parse(window.localStorage.data).id, // 用户ID
            'tokenId': token(),
            'invoiceId': this.billList[index].id,
            'companyName': this.billList[index].companyName,
            'dutyParagraph': this.billList[index].dutyParagraph,
            'companyAddress': this.billList[index].companyAddress,
            'companyPhone': this.billList[index].companyPhone,
            'state': 1 // 1表示默认选中  0表示不选中  （必填）
          }
      });
        this.$http.post(api + '/qqs.us', jsonValue).then((res) => {
          console.log(res);
          const data = res.data.p;
          if (data.isTrue === true) {
            this.getData();
//            //          this.$emit('has-log', res.data.username);
          } else {
            this.alertTip(data.errorMsg);
            if (data.errorCode === 90) {
              this.go= ()=> {
                this.showAlert = false
                let _this = this
                relogin(_this)
              }
            }
          }
        }, (res) => {
          this.alertTip('网络错误');
          this.go = ()=>{
             this.showAlert = false;
          }
        })
      },
      // 编辑
      edit (index) {
//        console.log(index)
//        console.log(this.address[index])
        this.$router.push({
          name: 'billEdit',
          params: {
            invoiceId: this.billList[index].id,
            companyName: this.billList[index].companyName,
            dutyParagraph: this.billList[index].dutyParagraph,
            companyAddress: this.billList[index].companyAddress,
            companyPhone: this.billList[index].companyPhone,
            state: this.billList[index].state  // 1表示默认选中  0表示不选中  （必填）
          }
        })
      },
      // 删除
      remove (index) {
        const jsonValue = JSON.stringify({
          'c': '10039',
          'p': {
            'userId': JSON.parse(window.localStorage.data).id, // 用户ID
            'tokenId': token(),
            'invoiceId': this.billList[index].id
          }
        });
        this.$http.post(api + '/qqs.us', jsonValue).then((res) => {
          console.log(res);
          const data = res.data.p;
          if (data.isTrue === true) {
            this.getData();
//            //          this.$emit('has-log', res.data.username);
          } else {
            this.showAlert = true;
            this.alertText = data.errorMsg
            if (data.errorCode === 90) {
              this.go= ()=> {
                this.showAlert = false
                let _this = this
                relogin(_this)
              }
            }
          }
        }, (res) => {
          this.showAlert = true;
          this.alertText = '网络错误'
        })
      },
      NewBill () {
        this.$router.replace({name:'billCreate'})
      }
    },
    created(){  wxHttpTool.wxAuthor(this, function (varRet, varJson, varThis = this) {
      if (varRet == -1) {
        varThis.alertTip('网络错误');
        varThis.go = function () {
          varThis.showAlert = false;
        }

      }
      // 关注钱应该判断下是否已经关注过了
      msg_10046.send(varThis,msg_10046.entity(),function(varThis,res){
        varThis.wxlist=res.data.p.sign
        wx_config(varThis.wxlist);
        wx_hideAllMenu();
        wx.hideMenuItems({
          //禁用单独分享朋友圈QQ空间等功能
          menuList: ["menuItem:share:timeline","menuItem:share:QZone","menuItem:share:qq"] // 要隐藏的菜单项，所有menu项见附录3
        });
      })


    },this.type)},
    mounted () {
//      location.reload()
      this.getData();
      // 判断对象是否为空对象
//      if (JSON.stringify(this.$route.params.pretermission) === 'true') {
      // 接收新建发票，判断是否默认
//      if (this.$route.params.pretermission === true) {
//        this.address.unshift(this.$route.params);
//      } else if (this.$route.params.pretermission === false) {
//        this.address.push(this.$route.params);
//      }
//      // 接收编辑发票
//      this.address[this.$route.params.index] = this.$route.params;
//      console.log(this.address[this.$route.params.index]);
//      console.log(this.address)
    }
//    beforeDestroy () {
//      // 保存默认发票
//      window.localStorage.pretermission = JSON.stringify(this.address[this.curId]);
//    }
  }
</script>

<style lang="scss" scoped>
  .content {
    background: #f4f4f4;
    height: 100%;
    .address {
      width: 100%;
      height: 100%;
      box-sizing: border-box;
      ul {
        padding-bottom: 120px;
        li {
          background: #ffffff;
          margin-bottom: 20px;
          .top {
            height: 208px;
            box-sizing: border-box;
            padding-top: 46px;
            font-size: 28px;
            color: #3b3b43;
            border-bottom: 1px solid #dcdcdc;
            .tTop{
              height: 40px;
              .name {
                float: left;
                padding-left: 34px;
              }
            }
            .dizhi {
              font-size: 26px;
              color: #a1a1a1;
              float: left;
              padding: 32px 30px 0;
            }
          }
          .bottom {
            position: relative;
            width: 100%;
            height: 90px;
            .select {
              float: left;
              padding-top: 26px;
              padding-left: 30px;
              b{
                font-weight: 400;
              }
              i{
                vertical-align: middle;
                display: inline-block;
                background: url('../assets/images/ico_2@1x.png') no-repeat;
                background-size: 100% 100%;
                width: 32px;
                height: 32px;
                margin-right: 8px;
              }
              .selected{
                background: url('../assets/images/ico_1@1x.png') no-repeat;
                background-size: 100% 100%;
              }
              .red{color: #ff0000}
            }
            .operate{
              width: 240px;
              height: 90px;
              float: right;
              padding-right: 14px;
              .right{
                padding-left: 42px;
              }
              div{
                float: left;
                padding-top: 26px;
                img{
                  vertical-align: middle;
                  width: 26px;
                  height: 26px;
                  padding-right: 12px;
                }
              }
            }
          }
        }
      }
      .btn{
        width:100%;
        height: 120px;
        text-align: center;
        line-height: 120px;
        background: #f4f4f4;
        position: fixed;
        bottom: 0;
        .btn-next{
          display: inline-block;
          width: 580px;
          height: 74px;
          margin:0;
          border-radius: 35px;
          border: 1px solid #fe3519;
          color: #fe3519;
          text-align: center;
          line-height: 74px;
          font-size: 26px;
        }
      }
    }
  }
</style>
